package cn.sc.pai.test;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;

public class Second1Activity extends AppCompatActivity {

    View content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().getDecorView().setBackgroundDrawable(null);
        setContentView(R.layout.activity_second1);
        content = findViewById(R.id.content);
    }

    float x = 0;//上一次
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float distance = 0;
        if (MotionEvent.ACTION_DOWN == action) {
            x = event.getX();
        } else if (MotionEvent.ACTION_MOVE == action) {
            distance = event.getX() - x;
            x = event.getX();
            content.scrollBy((int) -distance, 0);
        } else if (MotionEvent.ACTION_UP == action) {
            System.out.println("content.getScrollX()=" + content.getScrollX());
            if (Math.abs(content.getScrollX()) < content.getWidth() / 2) {
                startBeginAnimate();
            } else {
                startEndAnimate();
            }
        }
        return super.onTouchEvent(event);
    }

    private void startBeginAnimate() {
        ValueAnimator animator = ValueAnimator.ofInt(content.getScrollX(), 0);
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                content.scrollTo(value, 0);
            }
        });
        animator.start();
    }

    private void startEndAnimate() {
        ValueAnimator animator = ValueAnimator.ofInt(content.getScrollX(), -content.getWidth());
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                content.scrollTo(value, 0);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }
}
